﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Eventhere.Work;
using System.IO.IsolatedStorage;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Eventhere.Controls
{
    public partial class ListRepertory : UserControl
    {
        private Repertory rep = new Repertory();
        public ListRepertory()
        {
            InitializeComponent();
            if (IsolatedStorageSettings.ApplicationSettings.Contains("Repertoire"))
                this.rep = (Repertory)IsolatedStorageSettings.ApplicationSettings["Repertoire"];

            CreateGraphisme();
        }

        public void CreateGraphisme()
        {
            Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
            
            if (this.rep.Contacts.Count == 0)
            {
                TextBlock nocontact = new TextBlock();
                nocontact.Text = "No Contact";
                ContentRepertoryPanel.Children.Add(nocontact);
            }
            else
                ContentRepertoryPanel.Children.Clear();
            

            #region ajoutdeselementgraphiques
            Button espace = new Button();
            espace.BorderThickness = new Thickness(0);
            espace.Height = 25;
            ContentRepertoryPanel.Children.Add(espace);
            // le thème est sombre
            foreach (GroupContact g in this.rep.Groupes)// ON AFFICHE DABORD LES GROUPES
            {
                Button Unbutton = new Button();
                Unbutton.Content = g.NameGroupContact;
                Unbutton.FontWeight = FontWeights.Bold;
                Unbutton.Tag = g.IdGroup;
                Unbutton.Name = g.IdGroup.ToString();
                Unbutton.Background = new SolidColorBrush(Colors.DarkGray);
                if (g == this.rep.Groupes.First())
                    Unbutton.Margin = new Thickness(0, 0, 0, 0);
                else
                    Unbutton.Margin = new Thickness(0, -25, 0, 0);

                if (darkBackgroundVisibility == Visibility.Visible)
                    Unbutton.BorderBrush = new SolidColorBrush(Colors.White);
                else
                    Unbutton.BorderBrush = new SolidColorBrush(Colors.Black);
                Unbutton.Height = 100;
                Unbutton.BorderThickness = new Thickness(1);
                Unbutton.Padding = new Thickness(10, 0, 0, 0);
                Unbutton.UseLayoutRounding = true;
                Unbutton.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
                Unbutton.Click += ClickGroupContact;

                this.ContentRepertoryPanel.Children.Add(Unbutton);//Ajout du bouton au conteneur

            }

            foreach (Contact c in this.rep.Contacts)// ENSUITE LES CONTACTS
            {
                Button Unbutton = new Button();
                Unbutton.Tag = (int)c.IdContact;
                Unbutton.Content = c.MailContact;
                Unbutton.Margin = new Thickness(0, -25, 0, 0);
                if (darkBackgroundVisibility == Visibility.Visible) // theme sombre
                    Unbutton.BorderBrush = new SolidColorBrush(Colors.White);
                else
                    Unbutton.BorderBrush = new SolidColorBrush(Colors.Black); //theme clair
                Unbutton.Height = 100;
                Unbutton.BorderThickness = new Thickness(1);
                Unbutton.Padding = new Thickness(10, 0, 0, 0);
                Unbutton.UseLayoutRounding = true;
                Unbutton.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
                Unbutton.Click += ClickContact;
                

                Unbutton.ManipulationDelta += Unbutton_ManipulationDelta;


                this.ContentRepertoryPanel.Children.Add(Unbutton);
            }
            #endregion
        }

        void Unbutton_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
        {
            if (e.DeltaManipulation.Translation.X > 10)
            {
                Image myImage3 = new Image();
                BitmapImage bi3 = new BitmapImage();
                bi3.UriSource = new Uri("/img/Delete.png", UriKind.Relative);
                myImage3.Source = bi3;
                myImage3.Name = "deleteimage";
                myImage3.Opacity = 0;
                myImage3.Margin = new Thickness(350, 0, 0, 0);
                myImage3.Height = 26;
                myImage3.Width = 26;
                myImage3.Tag = ((Button)sender).Tag;
                myImage3.Tap += delete_click;
                ((Button)sender).Content = myImage3;
                
                MonStoryBoard.Begin();
            }
        }

        void delete_click(object sender, System.Windows.Input.GestureEventArgs e)
        {
            
            MessageBoxResult res = MessageBox.Show("Do you want to delete this Contact ?", "Are you sure?" , MessageBoxButton.OKCancel);
            if (res == MessageBoxResult.OK)
            {
                Repertory.DeleteContactById((int)((Image)sender).Tag);
                this.ContentRepertoryPanel.Children.Clear();
                CreateGraphisme();
            }
        }

        void ClickContact(object sender, RoutedEventArgs e)
        {
            clickcontact(sender, null);
        }
        void ClickGroupContact(object sender, RoutedEventArgs e)
        {
            clickgroupcontact(sender, null);
        }

        public event Action<object, EventArgs> clickcontact;
        public event Action<object, EventArgs> clickgroupcontact;
    }
}
